package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.HealthTestService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/healthTest")
public class HealthTestController {

    @Reference
    private HealthTestService healthTestService;


    //查询体检信息回显
    @RequestMapping("/find")
    public PageResult find(@RequestBody QueryPageBean queryPageBean) {
        return healthTestService.find(queryPageBean);
    }

    //删除体检信息
    @RequestMapping("/delete")
    public Result delete(Integer memberId) {
        try {
            healthTestService.delete(memberId);
            return new Result(true, MessageConstant.DELETE_HEALTHTEST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_HEALTHTEST_FAIL);
        }
    }

    //文件上传（Excel）
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> setmeal = new HashMap<>();
            String[] strings1 = list.get(1);
            Integer memberId1 = Integer.parseInt(strings1[0]);
            String memberName1 = strings1[1];
            Integer setmealId1 = Integer.parseInt(strings1[4]);
            String setmealName1 = strings1[5];
            setmeal.put("memberId", memberId1);
            setmeal.put("memberName", memberName1);
            setmeal.put("setmealName", setmealName1);
            setmeal.put("setmealId", setmealId1);

            Integer integer = healthTestService.findById(setmeal);
            if (integer > 0) {
                return new Result(false, MessageConstant.HEALTHTEST_FAIL);
            }
            healthTestService.add(setmeal);
            if (list != null && list.size() > 0) {
                for (String[] strings : list) {
                    if (strings[0] == null) {
                        break;
                    }
                    Integer memberId = Integer.parseInt(strings[0]);
                    String memberName = strings[1];
                    Integer checkitemId = Integer.parseInt(strings[2]);
                    String value = strings[3];
                    Integer setmealId = Integer.parseInt(strings[4]);
                    String setmealName = strings[5];

                    map.put("memberId", memberId);
                    map.put("memberName", memberName);
                    map.put("checkitemId", checkitemId);
                    map.put("value", value);
                    map.put("setmealId", setmealId);
                    map.put("setmealName", setmealName);
                    healthTestService.importHealthTest(map);
                    if (setmealId != setmealId1) {
                        healthTestService.add(map);
                    }
                }
            }
            return new Result(true, MessageConstant.IMPORT_HEALTHTEST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_HEALTHTEST_SUCCESS);
        }
    }

}
